//
//  LLMallTableViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/4.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMallTableViewController.h"
#import "LLMallCell.h"
#import "LLShopGoodsModel.h"
#import "LLMallHeaderView.h"
#import "LLGoodsDetailViewController.h"
#import "LLClusterBargainViewController.h"

@interface LLMallTableViewController ()<LLMallHeaderViewDelegate>
@property (copy, nonatomic) NSArray *doTextArr;
@property (assign, nonatomic) NSInteger selectedIndex;
@end

@implementation LLMallTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.doTextArr = @[@"立即抢购",@"去拼团",@"去砍价",@"去换购"];
//    [self initData];
    [self getProductData1];
}


- (void)configTableView {
    [super configTableView];
    self.tableView.height = LLScreenHeight-self.navH-50-self.tabH;
    [self.tableView registerCellByClassName:@"LLMallCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = LLScreenWidth < 375 ? 100 : 130;
    CGFloat left = LLScreenWidth < 375 ? 100 : 130;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, left, 0, 0);
    
    LLMallHeaderView *headerView = [[LLMallHeaderView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 80+LLScreenWidth*0.2+10)];
    headerView.delegate = self;
    headerView.backgroundColor = UIColor.whiteColor;
    self.tableView.tableHeaderView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LLMallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMallCell"];
    cell.model = self.models[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectedIndex == 2) {
        [self.navigationController pushViewController:LLClusterBargainViewController.new animated:YES];
    } else {
        LLGoodsDetailViewController *vc = LLGoodsDetailViewController.new;
        vc.model = self.models[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - LLMallHeaderViewDelegate

- (void)mallHeaderViewDidClickWithButton:(LLButton *)button {
    self.selectedIndex = button.tag;
    for (LLShopGoodsModel *model in self.models) {
        model.goods_name = self.doTextArr[button.tag];
    }
    [self.tableView reloadData];
}

/**
 获取商品数据
 */
-(void)getProductData1{
    NSDictionary *dic = @{@"mall_id":@"2",@"classify_second":self.dataDic[@"classify_id"],@"classify_first":@"0",@"page":@(self.page),@"type":@"1"};
    [self showSVProgressHUD];
    [LLHttpTool post:MALLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        [SVProgressHUD dismiss];
        NSArray *resultArray = [NSArray yy_modelArrayWithClass:[LLShopGoodsModel class] json:result[@"data"]];
        [self.models addObjectsFromArray:resultArray];
        [self.tableView reloadData];
       
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}
@end
